# NOTE: https://gist.github.com/bbqtd/a4ac060d6f6b9ea6fe3aabe735aa9d95 macos install tmux-256color
# set-option default-terminal "xterm-256color"
# set-option -a terminal-overrides ",*256col*:RGB"
set -g default-terminal "tmux-256color"
set-option -a terminal-overrides ",*256col*:RGB"
# set-option -sa terminal-features ',tmux-256color:RGB'
set-option -g status-position top


# action key
unbind C-b
set -g prefix C-a

setw -g xterm-keys on
set -s escape-time 0
set -sg repeat-time 300
set -s focus-events on
set -g mouse on
set -sg exit-empty on

set -q -g status-utf8 on
setw -q -g utf8 on

set -g visual-activity off
setw -g monitor-activity off
setw -g monitor-bell off

set -g history-limit 10000

# bind t send-key C-t
# Reload settings
bind r source-file ~/.config/tmux/tmux.conf \; display "Reloaded!"

# display
set -g base-index 1
setw -g pane-base-index 1

setw -g automatic-rename on
set -g renumber-windows on

# allow the title bar to adapt to whatever host you connect to
set -g set-titles on
set -g set-titles-string "#T"

set -g display-panes-time 2000
set -g display-time 2000

set -g status-interval 1

# create session
bind -n M-s new-session

# window management
bind -n M-w new-window -c "#{pane_current_path}"
# break pane to window
bind -n M-b break-pane
# close self pane
bind -n M-c kill-pane
# close other pane
bind -n M-o kill-pane -a
# close window
bind -n M-q kill-window
# close other window
bind -n M-e kill-window -a

#window navigation
unbind n
unbind p
unbind 1
unbind 2
unbind 3
unbind 4
unbind 5
unbind 6
unbind 7
unbind 8 
unbind 9
unbind 0
bind -n M-h previous-window
bind -n M-l next-window

# rebind split window
unbind %
bind | split-window -h

unbind '"'
bind \\ split-window -v

bind -n M-1 select-window -t 1
bind -n M-2 select-window -t 2
bind -n M-3 select-window -t 3
bind -n M-4 select-window -t 4
bind -n M-5 select-window -t 5
bind -n M-6 select-window -t 6
bind -n M-7 select-window -t 7
bind -n M-8 select-window -t 8
bind -n M-9 select-window -t 9

bind -n M-! join-pane -t :1
bind -n M-@ join-pane -t :2
bind -n 'M-#' join-pane -t :3
bind -n 'M-$' join-pane -t :4
bind -n M-% join-pane -t :5
bind -n M-^ join-pane -t :6
bind -n M-& join-pane -t :7
bind -n M-* join-pane -t :8
bind -n M-( join-pane -t :9

bind -r m resize-pane -Z

# pane navigation
bind 1 select-pane -t:.1
bind 2 select-pane -t:.2
bind 3 select-pane -t:.3
bind 4 select-pane -t:.4
bind 5 select-pane -t:.5
bind 6 select-pane -t:.6
bind 7 select-pane -t:.7
bind 8 select-pane -t:.8
bind 9 select-pane -t:.9
bind 0 select-pane -t:.10
bind > swap-pane -D
bind < swap-pane -U
bind -n M-Space next-layout


bind W choose-tree -Z
bind H choose-tree 'move-pane -v -s "%%"'
bind V choose-tree 'move-pane -h -s "%%"'

# set -g status-keys emacs
set -g mode-keys vi

# vim-like pane switching
# bind -r k select-pane -U 
# bind -r j select-pane -D 
# bind -r h select-pane -L 
# bind -r l select-pane -R 

# bind -n M-h select-pane -L
# bind -n M-j select-pane -D
# bind -n M-k select-pane -U
# bind -n M-l select-pane -R

# bind-key -n M-Right resize-pane -R 5
# bind-key -n M-Left resize-pane -L 5
# bind-key -n M-Up resize-pane -U 5
# bind-key -n M-Down resize-pane -D 5
#
bind -n M-v copy-mode

bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi C-v send-keys -X rectangle-toggle
bind -T copy-mode-vi h send-keys -X cursor-left
bind -T copy-mode-vi l send-keys -X cursor-right
bind -T copy-mode-vi k send-keys -X cursor-up
bind -T copy-mode-vi j send-keys -X cursor-down
bind -T copy-mode-vi e send-keys -X next-word-end
bind -T copy-mode-vi K send-keys -N 5 -X cursor-up
bind -T copy-mode-vi J send-keys -N 5 -X cursor-down
bind -T copy-mode-vi H send-keys -X start-of-line
bind -T copy-mode-vi L send-keys -X end-of-line
bind -T copy-mode-vi Y send-keys -X copy-end-of-line
bind -T copy-mode-vi y send-keys -X copy-selection-and-cancel
bind -T copy-mode-vi = send-keys -X search-again
bind -T copy-mode-vi = send-keys -X search-reverse

bind b list-buffers
bind p paste-buffer

set -g set-clipboard on

# Open current directory
bind o run-shell "open #{pane_current_path}"


# Moving window
bind-key -n M-j swap-window -t -1 \; previous-window
bind-key -n M-k swap-window -t +1 \; next-window
# bind-key -r C-h swap-window -t -1 \; previous-window
# bind-key -r C-l swap-window -t +1 \; next-window


# tpm plugin
# set -g @plugin 'tmux-plugins/tpm'
# #
# set -g @plugin 'christoomey/vim-tmux-navigator'
# NOTE: git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# NOTE：安装插件 <C-a> + I
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-cpu'

#### basic settings
set-option -g status-justify "left"
#set-option utf8-default on
#set-option -g mouse-select-pane
set-window-option -g mode-keys vi
#set-window-option -g utf8 on
# look'n feel
set-option -g status-fg cyan
set-option -g status-bg black
set -g pane-active-border-style fg=colour166,bg=default
set -g window-style fg=colour10,bg=default
set -g window-active-style fg=colour12,bg=default


#### COLOUR

# default statusbar colors
set-option -g status-style bg=colour235,fg=colour136,default

# default window title colors
set-window-option -g window-status-style fg=colour244,bg=colour234,dim

# active window title colors
set-window-option -g window-status-current-style fg=colour166,bg=default,bright

# pane border
set-option -g pane-border-style fg=colour235 #base02
set-option -g pane-active-border-style fg=colour136,bg=colour235

# message text
set-option -g message-style bg=colour235,fg=colour166

# pane number display
set-option -g display-panes-active-colour colour33 #blue
set-option -g display-panes-colour colour166 #orange

# clock
set-window-option -g clock-mode-colour colour64 #green


# import
if-shell "uname -s | grep -q Darwin" "source ~/.config/tmux/macos.conf"

source ~/.config/tmux/statusline.conf
source ~/.config/tmux/utility.conf

set -g @plugin 'aserowy/tmux.nvim'

# navigation
set -g @tmux-nvim-navigation-cycle true
set -g @tmux-nvim-navigation true
set -g @tmux-nvim-navigation-keybinding-left 'C-h' 
set -g @tmux-nvim-navigation-keybinding-down 'C-j' 
set -g @tmux-nvim-navigation-keybinding-up 'C-k' 
set -g @tmux-nvim-navigation-keybinding-right 'C-l' 

# resize
set -g @tmux-nvim-resize true
set -g @tmux-nvim-resize-step-x 1
set -g @tmux-nvim-resize-step-y 1
set -g @tmux-nvim-resize-keybinding-left 'C-Left' 
set -g @tmux-nvim-resize-keybinding-down 'C-Down' 
set -g @tmux-nvim-resize-keybinding-up 'C-Up' 
set -g @tmux-nvim-resize-keybinding-right 'C-Right'

# is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
#
# bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h' { if -F '#{pane_at_left}' '' 'select-pane -L' }
# bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j' { if -F '#{pane_at_bottom}' '' 'select-pane -D' }
# bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k' { if -F '#{pane_at_top}' '' 'select-pane -U' }
# bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l' { if -F '#{pane_at_right}' '' 'select-pane -R' }
#
# bind-key -T copy-mode-vi 'C-h' if -F '#{pane_at_left}' '' 'select-pane -L'
# bind-key -T copy-mode-vi 'C-j' if -F '#{pane_at_bottom}' '' 'select-pane -D'
# bind-key -T copy-mode-vi 'C-k' if -F '#{pane_at_top}' '' 'select-pane -U'
# bind-key -T copy-mode-vi 'C-l' if -F '#{pane_at_right}' '' 'select-pane -R'

# is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

# bind -n 'C-Left' if-shell "$is_vim" 'send-keys C-Left' 'resize-pane -L 1'
# bind -n 'C-Down' if-shell "$is_vim" 'send-keys C-Down' 'resize-pane -D 1'
# bind -n 'C-Up' if-shell "$is_vim" 'send-keys C-Up' 'resize-pane -U 1'
# bind -n 'C-Right' if-shell "$is_vim" 'send-keys C-Right' 'resize-pane -R 1'
#
# bind-key -T copy-mode-vi C-Left resize-pane -L 1
# bind-key -T copy-mode-vi C-Down resize-pane -D 1
# bind-key -T copy-mode-vi C-Up resize-pane -U 1
# bind-key -T copy-mode-vi C-Right resize-pane -R 1

run '~/.tmux/plugins/tpm/tpm'
